package leetcode;

public class LC31 {
    public static void main(String[] args) {
        LC31 lc31 = new LC31();
        ListNode.print(lc31.reverseKGroup(ListNode.ln(new int[]{1,2,3,4,5}),3));
        ListNode.print(lc31.reverseKGroup(ListNode.ln(new int[]{1,2,3,4,5}),4));
    }
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode ans = new ListNode(0,head);
        ListNode temp = ans;
        while(temp != null){
            ListNode x = temp.next;
            for (int i = 0; i < k; i++) {
                if (x == null) return ans.next;
                x = x.next;
            }
            x = temp.next;
            ListNode z;
            for (int i = 1; i < k; i++) {
                ListNode y = x.next;
                x.next = y.next;
                z = temp.next;
                temp.next = y;
                y.next = z;
            }
            temp = x;
        }
        return ans.next;
    }
}
